var MainMenuLayer = cc.Layer.extend({
    _util: new Util(),
    _mainMenu: null,
    _optionLayer: null,
    _playBtn: null,
    _sprGameLogo: {
        key: "sprGameLogo"
    },
    _manage: null,
    _anim: "In",
    animateCallback: null,
    ctor: function(){
        this._super();
        if ('touches' in sys.capabilities) 
            this.setTouchEnabled(true);
        else if ('mouse' in sys.capabilities) 
            this.setMouseEnabled(true);
    },
    init: function(){
        var bRet = false;
        if (this._super()) {
            util.i18n.members = new Array();
            this._util.onAssignCCBMemberVariable(this.assignActive());
            var mainMenu = this._util.newLayerFromCCB(s_mainMenu_ccbi, this);
            this._manage = this._util.ccbReader.getAnimationManager();
            this._manage.setCompletedAnimationCallback(this, this.onCCBAnimEnd);
            this._manage.runAnimations(this._anim);
            
            if (mainMenu != null) {
                this.addChild(mainMenu);
                this._mainMenu = mainMenu;
                this._optionLayer = OptionLayer.create(this);
                this.addChild(this._optionLayer, 4);
                this.changeGameLogo();
            }
            
            bRet = true;
        }
        return bRet;
    },
    outFunc: null,
    onCCBAnimEnd: function(){
        var animName = this._manage.getLastCompletedSequenceName();
        if (animName == "Out") {
            if (this.outFunc != null) {
                this.outFunc();
            }
        }
    },
    assignActive: function(){
        var arr = new Array();
        var $this = this;
        this._playBtn = {
            key: "btnPlay",
            callback: function(){
                sound.playButtonSound();
                if ($this._manage.getRunningSequenceName() == null) {
                    $this._manage.runAnimations("Out");
                    $this.outFunc = function(){
                        if (config.hasPlayedLevelOne() && !config.throughLevel) {
                            game.enterSelectLevels();
                        } else {
                            game.playLevel(1);//第一关
                        }
                    };
                }
            }
        }
        arr.push(this._playBtn);
        arr.push(this._sprGameLogo);
        arr.push({
            key: "btnMoreApp",
            callback: function(){
                sound.playButtonSound();
                cc.log("btnMoreApp")
            }
        });
        arr.push({
            key: "btnOption",
            callback: function(){
                sound.playButtonSound();
                if ($this._optionLayer._manage.getRunningSequenceName() == null) {
                    if ($this._optionLayer.isOpened()) {
                        $this._optionLayer.onClose();
                    } else {
                        $this._optionLayer.onOpen();
                        $this._playBtn.instance.setEnabled(false);
                    }
                }
            }
        });
        return arr;
    },
    changeGameLogo: function(){
        var logo_png = s_logo;
        var size = cc.size(623, 441);
        if (util.i18n.languageMatch(config.getCurrentLanguage()) == 'zh-Hans') {
            logo_png = s_logoCh;
            size = cc.size(564, 350);
        }
        var image = new Image();
        image.src = logo_png;
        image.width = size.width;
        image.height = size.height;
        var texture2d = new cc.Texture2D();
        texture2d.initWithElement(image);
        texture2d.handleLoadedTexture();
        this._sprGameLogo.instance.setDisplayFrame(cc.SpriteFrame.createWithTexture(texture2d, cc.rect(0, 0, size.width, size.height)));
    },
    onSwitchLanguage: function(){
        this.changeGameLogo();
    },
    onMouseUp: function(event){
        cc.log(event.getLocation())
        this.onLayerTouch(event);
    },
    onTouchesEnded: function(touches, event){
        if (touches.length <= 0) 
            return;
        var touch = touches[0];
        this.onLayerTouch(touch);
    },
    onLayerTouch: function(event){
        if (this._optionLayer.isOpened() && this._optionLayer._preventDefault) {
            this._optionLayer.onClose();
        }
        this._optionLayer._preventDefault = true;
    }
});

MainMenuLayer.create = function(){
    var sl = new MainMenuLayer();
    if (sl && sl.init()) {
        return sl;
    }
    return null;
}

MainMenuLayer.scene = function(){
    var scene = cc.Scene.create();
    var layer = MainMenuLayer.create();
    scene.addChild(layer);
    sound.playBGM();
    return scene;
}

var OptionLayer = cc.Layer.extend({
    _util: new Util(),
    _isOpened: false,
    _mangae: null,
    _superLayer: null,
    _preventDefault: true,
    _musicBtn: null,
    _langBtn: null,
    init: function(superLayer){
        this._superLayer = superLayer;
        var bRet = false;
        if (this._super()) {
            this._util.onAssignCCBMemberVariable(this.assignActive());
            var option = this._util.newLayerFromCCB(s_mainMenuRing_ccbi, superLayer._mainMenu);
            this._manage = this._util.ccbReader.getAnimationManager();
            this._manage.setCompletedAnimationCallback(this, this.onCCBAnimEnd);
            if (option != null) {
                this.addChild(option);
                this.setVisible(false)
            }
            game.updateSoundButton(this._musicBtn.instance);
            game.updateLanguageButton(this._langBtn.instance);
            bRet = true;
        }
        return bRet;
    },
    assignActive: function(){
        var arr = new Array();
        var $this = this;
        this._musicBtn = {
            key: "btnMusic",
            callback: function(){
                sound.playButtonSound();
                $this._preventDefault = false;
                $this.onMusic(this);
            }
        }
        this._langBtn = {
            key: "btnLang",
            callback: function(){
                sound.playButtonSound();
                $this._preventDefault = false;
                $this.onSwitchLanguage(this);
            }
        }
        arr.push(this._langBtn);
        arr.push(this._musicBtn);
        arr.push({
            key: "btnHelp",
            callback: function(){
                sound.playButtonSound();
                cc.log("btnHelp")
            }
        });
        return arr;
    },
    isOpened: function(){
        return this._isOpened;
    },
    onOpen: function(){
        this.setVisible(true);
        this._manage.runAnimations("In");
        this.outAction = null;
    },
    onClose: function(){
        this._manage.runAnimations("Out");
        this._superLayer._playBtn.instance.setEnabled(true);
        this.outAction = function(){
            this._isOpened = false;
            this.setVisible(false);
        }
    },
    onMusic: function(btnMusic){
        game.switchSoundOnOff();
        game.updateSoundButton(btnMusic);
    },
    onSwitchLanguage: function(btnLang){
        game.switchLanguage();
        game.updateLanguageButton(btnLang);
        this._superLayer.changeGameLogo();
    },
    outAction: null,
    onCCBAnimEnd: function(){
        var animName = this._manage.getLastCompletedSequenceName();
        if (animName == "Out") {
            if (this.outAction != null) {
                this.outAction();
            }
        } else if (animName == "In") {
            this._isOpened = true;
        }
    }
});

OptionLayer.create = function(superLayer){
    var sl = new OptionLayer();
    if (sl && sl.init(superLayer)) {
        return sl;
    }
    return null;
}
